package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * TODO 这是MVC的配置类
 * @author Yanxinhao on 2024/11/16
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    // 自动注入我们编写的转换器工厂类
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    //自动注入我们编写的自定义JWT拦截器类
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    // 自动注入我们配置的包含和排除路径(用于拦截器拦截的路径以及不拦截的路径)
    @Value("${admin.auth.path-patterns.include}")
    private String[] includePathPatterns;
    @Value("${admin.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;

    /**
     * addFormatters方法是用于添加格式化配置项的方法
     * @param registry 格式化注册表
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //TODO 我们向格式化注册表中添加了自定义的转换器工厂，用于将字符串转换为枚举类型
        registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }

    /**
     * 注册拦截器--for JWT令牌验证
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns(includePathPatterns)
                .excludePathPatterns(excludePathPatterns);
    }
}